/*
 * Copyright (C) 2011 bus.fm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.busfm.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * Title:PlayList
 * </p>
 * <p>
 * Description: PlayList
 * </p>
 * <p>
 * Copyright (c) 2011 www.bus.fm Inc. All rights reserved.
 * </p>
 * <p>
 * Company: bus.fm
 * </p>
 * 
 * 
 * @author jingguo0@gmail.com
 * 
 */
public class PlayList {
    private ArrayList<SongEntity> playlist;
    private int selected = 0;

    public PlayList() {
        playlist = new ArrayList<SongEntity>();
    }

    public void addList(SongEntity songEntity) {
        if (null != songEntity) {
            playlist.add(songEntity);
        }
    }

    public void addList(List<SongEntity> songEntities) {
        if (null != songEntities) {
            playlist.addAll(songEntities);
        }
    }

    public ArrayList<SongEntity> getPlayList() {
        return playlist;
    }

    public boolean isEmpty() {
        return playlist.size() == 0;
    }

    public int size() {
        return playlist == null ? 0 : playlist.size();
    }

    public void selectNext() {
        if (!isEmpty()) {
            selected++;
            selected %= playlist.size();
        }
    }

    public void selectPrev() {
        if (!isEmpty()) {
            selected--;
            if (selected < 0)
                selected = playlist.size() - 1;
        }
    }

    public void select(int index) {
        if (!isEmpty()) {
            if (index >= 0 && index < playlist.size())
                selected = index;
        }
    }

    public int getSelectedIndex() {
        if (isEmpty()) {
            selected = -1;
        }
        if (selected == -1 && !isEmpty()) {
            selected = 0;
        }
        return selected;
    }

    public SongEntity getSelectedEntity() {
        if (playlist != null && playlist.size() >= selected)
            return playlist.get(selected);
        return null;
    }
}
